package com.itboye.hutouben.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;

import com.itboye.hutouben.R;
import com.itboye.hutouben.base.MyApplcation;
import com.itboye.hutouben.choosecity.CityModel;
import com.itboye.hutouben.choosecity.db.DBHelper;
import com.itboye.hutouben.choosecity.db.RecordSQLiteOpenHelper;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

/**
 * Created by itboye on 2016/12/27.
 */

public class DBManager {
    DBHelper mDbHelper;
    RiDBHellp dbHellp;
    SQLiteDatabase db;
    Context mContext;
    private RecordSQLiteOpenHelper helper = new RecordSQLiteOpenHelper(MyApplcation.ctx);;
    public static final String DB_NAME = "itboye.db";
    public static final String DB_PATH = "/data" + Environment.getDataDirectory().getAbsolutePath() + "/" + "com.itboye.sunsunhome" ; // 存放路径
    private final int BUFFER_SIZE = 400000;
    public DBManager(Context mContext) {
        this.mContext=mContext;
        mDbHelper = new DBHelper(mContext);
        dbHellp=new RiDBHellp(mContext);
    }

    /***
     * 添加进草稿
     * @param uid
     * @param title
     * @param content
     * @param img
     * @return
     */
    public long addNote(String uid,String title, String content,String img) {
        db = mDbHelper.getWritableDatabase();
        ContentValues cv = new ContentValues();
        cv.put("uid", uid);
        cv.put("title", title);
        cv.put("content", content);
        cv.put("img", img);
        long num = db.insert("mynote", null, cv);
        db.close();
        return num;
    }



    /***
     * 添加进草稿
     * @param uid
     * @param title
     * @param content

     * @return
     */
    public long addRich(String uid,String title, String content,String imgid) {
        db = mDbHelper.getWritableDatabase();
        ContentValues cv = new ContentValues();
        cv.put("uid", uid);
        cv.put("title", title);
        cv.put("content", content);
        cv.put("imgid", imgid);
//		cv.put("img", img);
        long num = db.insert("mynotee", null, cv);
        db.close();
        return num;
    }



    /**
     * 被调用方法
     */
    public void openDateBase()
    {
        this.db = this.openDateBase(DB_PATH + "/" + DB_NAME);

    }
    /**
     * 被调用方法
     */
    public void closeDateBase()
    {
        if(this.db!=null)
            this.db.close();

    }


    /**
     * 打开数据库
     *
     * @param dbFile
     * @return SQLiteDatabase
     * @author sy
     */
    private SQLiteDatabase openDateBase(String dbFile){
        File file = new File(dbFile);
        if (!file.exists())
        {
            // // 打开raw中得数据库文件，获得stream流
            InputStream stream = mContext.getResources().openRawResource(R.raw.itboye);
            try
            {

                // 将获取到的stream 流写入道data中
                FileOutputStream outputStream = new FileOutputStream(dbFile);
                byte[] buffer = new byte[BUFFER_SIZE];
                int count = 0;
                while ((count = stream.read(buffer)) > 0)
                {
                    outputStream.write(buffer, 0, count);
                }
                outputStream.close();
                stream.close();
                SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbFile, null);
                return db;
            } catch (FileNotFoundException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return db;
    }
    public  String queryCityNo(String cityName){
//		db = mDbHelper.getReadableDatabase();
        Cursor cursor=null;
        if(this.db!=null)
            cursor = db.query("hat_city", new String[]{"cityID"}, "city=?", new String[]{cityName}, null	,null, null);
        String cityCode = null;
        if (cursor != null)
            while(cursor.moveToNext()){
                cityCode = cursor.getString(0);
            }
        cursor.close();
        db.close();
        return cityCode;
    }
    /***
     * 查询区域列表
//     * @param cityName
     * @return
     */
    public ArrayList<CityModel> queryArea(String cityNo){

        Cursor	cursor=null;
        ArrayList<CityModel> arrayArea=new ArrayList<>();
        if(this.db!=null)
//			cursor=db.rawQuery(", selectionArgs)
            cursor = db.query("hat_area", null, "father like ?", new String[]{cityNo+"%"}, null	,null, null);
        if (cursor != null)
            while(cursor.moveToNext()){
                CityModel cityModel=new CityModel();
                cityModel.setAreaCode(cursor.getString(cursor.getColumnIndexOrThrow("areaID")));
                cityModel.setCityCode(cursor.getString(cursor.getColumnIndexOrThrow("father")));
                cityModel.setAreaName(cursor.getString(cursor.getColumnIndexOrThrow("area")));
                arrayArea.add(cityModel);
            }
        cursor.close();
        db.close();
        return arrayArea;
    }

    public String queryPriovinceNo(String city) {
        // TODO Auto-generated method stub

        db = mDbHelper.getReadableDatabase();
        Cursor	cursor=null;
        if(this.db!=null)
            cursor = db.query("hat_province", new String[]{"provinceID"}, "province=?", new String[]{city}, null	,null, null);
        String cityCode = null;
        if (cursor != null)
            while(cursor.moveToNext()){
                cityCode = cursor.getString(0);
            }
        cursor.close();
        db.close();
        return cityCode;
    }

    /**
     * 清空数据
     */
    public void deleteData() {
        db = helper.getWritableDatabase();
        db.execSQL("delete from records");
        db.close();
    }
}
